package com.bit.cfdi.web.validators.common;

import org.zkoss.bind.ValidationContext;
import org.zkoss.bind.validator.AbstractValidator;

import com.bit.cfdi.web.constants.CFDIConstants;

public class PasswordStrengthValidator extends AbstractValidator {
	
	private String passwordFieldName;
	
	public PasswordStrengthValidator(final String passwordFieldName) {
		this.passwordFieldName = passwordFieldName;
	}

	@Override
	public void validate(final ValidationContext ctx) {
		String passwordToValidate = (String) ctx.getProperties(
				passwordFieldName)[0].getValue();
		if(passwordToValidate == null || passwordToValidate.trim().isEmpty()) {
			return;
		}
		if(!passwordToValidate.matches(CFDIConstants.PASSWORD_EXPRESSION)) {
			addInvalidMessage(ctx, passwordFieldName, 
					CFDIConstants.PASSWORD_REQUIREMENTS);
		}
	}

}
